//g++ mongohlp.cc -lboost_program_options
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <boost/functional/hash.hpp>
#include <google/sparse_hash_map>
#include <boost/program_options.hpp>
#include <re2/re2.h>
#include <stdio.h>
#include <string>
#include <stdlib.h>
#include "client/dbclient.h"
#include "phraser.h"
using namespace std;
using namespace mongo;
using namespace zxlib;

typedef google::sparse_hash_map<string, int, boost::hash<string> > Dict;
typedef Dict::value_type Item;

int parse_cmdline( cmd_line &cmd_line,int argc,char*argv[])
{
     bool error=false;
     boost::program_options::options_description options("command line options");
     boost::program_options::variables_map option_map;
     options.add_options() 
          ("help,h", "show this usage information")
          ("1,1", po::value<bool>(&cmd_line.one_column), "only print word column")
          ("version", "show version information")
          ("limit,n", po::value<int>(&cmd_line.limit), "count limit to process")
          ("output,o", po::value<string>(), "output file")
          ("config,c", po::value<string>(), "config file")
          ("zconfig,z", po::value<string>(), "ztexting config file");
     try {
          boost::program_options::store( boost::program_options::parse_command_line(argc, argv, options), option_map);
          boost::program_options::notify(option_map);

          if (option_map.count("help")) {
               cout << options << endl;
          }
          const char *required[]={"config","zconfig","output"};
          const char *required_desc[]={"config file","ztexting config file","output file"};
          string* params[]={ &cmd_line.config_file,&cmd_line.ztexting_config_file,&cmd_line.output_file };
          for( size_t i=0 ; i< sizeof(required)/sizeof(char*); i++ ){
               if( !option_map.count(required[i]) ) {
                    error= true;
                    cout << "** " << required_desc[i] << " required" << endl;
               } else {
                    params[i]->append(option_map[required[i]].as<string>());
               }
          }
          if( error ){
               cout << options << endl;
          } else {
          }
     } catch(po::error &e){
          cout << "ERROR: " << e.what() << endl << endl;
          cout << options << endl;
          return false;
     }
     return !error;

}
int main (int argc, char* argv[])
{
     cmd_line cmd_line;
     if( parse_cmdline(cmd_line,argc,argv) ){
          ztexting::init(cmd_line.ztexting_config_file.c_str());
          phraser(cmd_line).by_stats();
     }
     return 0;
}

